// pages/near-other/near-other.js
const app = getApp()
Page({
    data: {
        page: 1,
        nextPage: true,
        loading: false,
        list: [],
    },

    onLoad: function(options) {

    },
    onShow() {
        let address = wx.getStorageSync('address')
        let location = wx.getStorageSync('location')
        if (!address && !location) {
            wx.navigateBack({
                delta: 1
            })
            return
        }
        this.setData({
            address,
            location,
			page: 1,
			nextPage: true,
			loading: false,
			list: [],
        })
        this.loadData()
    },
    loadData() {
        let {
            page = 1, nextPage = true, loading = false, list = [], location = {}
        } = this.data
        if (loading || !nextPage) {
            return
        }
        this.setData({
            loading: true
        })
        app.http.get('location/community-list', {
            data: {
                location: location.lng + ',' + location.lat,
                page: page
            }
        }).then(res => {
            page++
            nextPage = res.data.nextStatus
            loading = false
            list.push(...res.data.list)
            this.setData({
                page,
                nextPage,
                loading,
                list
            })
            wx.stopPullDownRefresh()
        }).catch(err => {
            this.setData({
                loading: false
            })
        })
    },
    chooseSq(evt) {
        let page = getCurrentPages()
        let indexPage = page[page.length - 3]
        app.cid = evt.currentTarget.dataset.cid
		wx.setStorageSync('cid', evt.currentTarget.dataset.cid)
        // app.toPromise(wx.setStorage, {
        //     key: 'cid',
        //     data: evt.currentTarget.dataset.cid,
        // }).then(res => {
            if (indexPage.route == 'pages/index/index') {
                const tab = indexPage.tab()
                tab.page = 1
                tab.list = []
                tab.writeOffList = []
                tab.nextPage = true
                tab.loading = false
                indexPage.onLoad()
            }
            wx.switchTab({
                url: '/pages/index/index',
            })
        // })
    },
    onPullDownRefresh: function() {
        this.setData({
            page: 1,
            nextPage: true,
            loading: false,
            list: []
        })
        this.loadData()
    },

    onReachBottom: function() {
        this.loadData()
    },
})